package com.study.studyroom.interceptor;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.study.studyroom.entity.enums.RequestResultEnum;
import com.study.studyroom.utils.ResponseUtil;
import com.study.studyroom.utils.TokenUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
@AllArgsConstructor
@Slf4j
public class AuthenticationInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String token = request.getHeader("token");
        //如果token不是空的 进行token解析 解析出用户的ID
        if (StringUtils.isNotBlank(token)){
            String userId = TokenUtil.getUserIdByToken(token);
            String roleId = TokenUtil.getRoleIdByToken(token);
            log.info("身份信息处理,当前携带token:{},解析结果=>用户ID:{},用户角色ID:{}",token,userId,roleId);
        }else{
            ResponseUtil.returnRequestEnum(RequestResultEnum.LOGIN_EXPIRED);
            return false;
        }
        return true;
    }

}
